package com.springboot.fram.core.utils;

import com.springboot.fram.core.date.DatePattern;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * @author liheng
 * @ClassName StringUtils
 * @Description
 * @date 2021-08-13 10:01
 */
public class StringUtils {
    private static int DEF_GENERATOR_LEN = 14;

    /**
     * 首字母转小写
     *
     * @param field
     * @return
     */
    public static String toLowerCaseFirstOne(String field) {
        if (Character.isLowerCase(field.charAt(0))) {
            return field;
        } else {
            char firstOne = Character.toLowerCase(field.charAt(0));
            String other = field.substring(1);
            return new StringBuilder().append(firstOne).append(other).toString();
        }
    }

    /**
     * 长度必须大于0
     *
     * @param length
     */
    private static void len(int length) {
        if (length <= 0) {
            throw new IllegalArgumentException(
                    "The length has to be greater than 0");
        }
    }

    /**
     * 生成指定长度的数字随机字符串，大于14位追加时间格式
     *
     * @param len
     * @return
     */
    public static String generatorNumberStr(int len) {
        len(len);
        String seq;
        if (len <= DEF_GENERATOR_LEN) {
            seq = String.valueOf((long) (1.0D + Math.random() * Math.pow(10.0D, (double) (len + 5)))).substring(0, len);
        } else {
            BigInteger random = (new BigDecimal(1)).add((new BigDecimal(Math.random())).multiply((new BigDecimal(10)).pow(len - 14 + 5))).toBigInteger();
            seq = (new SimpleDateFormat(DatePattern.DATETIME_FILE.getPattern())).format(new Date()) + random.toString().substring(0, len - 14);
        }
        return seq;
    }

    /**
     * 生成指定长度的数字随机字符串，大于14位追加时间格式
     * 且以0补位
     *
     * @return
     */
    public static String getNumberStrByZero() {
        return getNumberStrByZero(24);
    }

    /**
     * 生成指定长度的数字随机字符串，大于14位追加时间格式
     * 且以0补位
     *
     * @param len
     * @return
     */
    public static String getNumberStrByZero(int len) {
        len(len);
        if (len <= DEF_GENERATOR_LEN) {
            return String.valueOf((long) (1.0D + Math.random() * Math.pow(10.0D, (double) (len + 5)))).substring(0, len);
        }
        Date date = new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmm");
        String machineId = df.format(date);
        // 取绝对值
        int hashCodeV = Math.abs(UUID.randomUUID().toString().hashCode());
        String ordernum = machineId + String.format("%0" + (len - 12) + "d", hashCodeV);
        return ordernum.substring(0, len);
    }


    /**
     * string2Bytes
     * @param str
     * @return
     */
    public static byte[] string2Bytes(String str) {
        if (str == null || str.equals("")) {
            return null;
        }
        str = str.toUpperCase();
        int length = str.length() / 2;
        char[] strChar = str.toCharArray();
        byte[] bt = new byte[length];

        for (int i = 0; i < length; i++) {
            int index = i * 2;
            bt[i] = (byte) (char2Byte(strChar[index]) << 4 | char2Byte(strChar[index + 1]));
        }
        return bt;
    }

    private static byte char2Byte(char ch) {
        return (byte) "0123456789ABCDEF".indexOf(ch);
    }

    /**
     * stringToAscii
     * @param hex
     * @return
     */
    public static String stringToAscii(String hex) {
        String ret = "";
        byte[] baKeyword = new byte[hex.length() / 2];
        for (int i = 0; i < baKeyword.length; i++) {
            try {
                baKeyword[i] = (byte) (0xff & Integer.parseInt(
                        hex.substring(i * 2, i * 2 + 2), 16));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        try {
            ret = new String(baKeyword, "UTF-8");
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return ret;
    }

    public static void main(String[] args) {
        System.out.println(getNumberStrByZero());
    }

}
